package com.xja.shiro;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;


import com.xja.pojo.Admin;
import com.xja.service.AdminService;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.pam.UnsupportedTokenException;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.InvalidDataAccessResourceUsageException;
import org.springframework.jdbc.JdbcUpdateAffectedIncorrectNumberOfRowsException;

public class AuthenticationRealm extends AuthorizingRealm{

	@Autowired
	private AdminService adminService;

	@Override
	protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
		Principal principal=(Principal) principals.fromRealm(getName()).iterator().next();
		if(principal!=null) {
			List<String> permissionURL = adminService.selectAdminPermissionById(principal.getId());
			if(permissionURL!=null) {
				SimpleAuthorizationInfo authorizationInfo=new SimpleAuthorizationInfo();
				authorizationInfo.addStringPermissions(permissionURL);
				return authorizationInfo;
			}
		}
		return null;
	}

	@Override
	protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
		AuthenticationToKen authenticationToKen=(AuthenticationToKen) token;
		String loginName=authenticationToKen.getUsername();
		String password=new String(authenticationToKen.getPassword());
		System.out.println(loginName+"    "+password);
		if(!authenticationToKen.isValid()) {
			throw new UnsupportedTokenException();
		}
		if(loginName!=null&&!"".equals(loginName)) {
			Admin loginPOJO = adminService.selectAdminByLoginName(loginName);
			System.out.println(loginPOJO);
			if(loginPOJO==null) {
				throw new UnknownAccountException();
			}
			if(!loginPOJO.getPwd().equals(password)) {
				//有用户，信息错误 或者 用户锁定
				Integer failcount = loginPOJO.getFailcount();
				if(failcount<3){
					loginPOJO.setFailcount(failcount+1);
					loginPOJO.setLogindate(new Date());
					adminService.updateAdmin(loginPOJO);
				}else{
					loginPOJO.setFailcount(0);
					loginPOJO.setLogindate(new Date());
					long currentTime = System.currentTimeMillis() ;
					currentTime +=3*60*1000;
					Date date=new Date(currentTime);
					/*SimpleDateFormat dateFormat = new SimpleDateFormat(
							"yyyy-MM-dd HH:mm:ss");
					System.out.println(dateFormat.format(date));*/
					loginPOJO.setLockexpiredate(date);
					adminService.updateAdmin(loginPOJO);
				}
				throw new UnknownAccountException();
			}
			Date newDate = new  Date();
			Date lockexpiredate = loginPOJO.getLockexpiredate();
			System.out.println(!loginPOJO.getPwd().equals(password)+"   "+newDate.before(lockexpiredate));
			if(newDate.before(lockexpiredate)){
				throw new IncorrectCredentialsException();
			}
			System.out.println("成功");
			//登录成功，更新用户信息
			loginPOJO.setLogindate(new Date());
			loginPOJO.setFailcount(0);
			adminService.updateAdmin(loginPOJO);
			return new SimpleAuthenticationInfo(new Principal(loginPOJO.getId(),loginPOJO.getLoginname()), password, getName());
		}
		return null;
	}

	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
}
